<?php
/**
 * display all modules in a unit in a table 2 rows and 6 colunms
 * display two video files and urls, questions and answers and submit answer button
 * if there are no video files, create table with 1 colunm
 */
?>
<h1>Overall Unit Description and Module Number</h1>
<?php
	$status=$unitstatus;
	$modules=array_chunk($cdeModules,6);
?>

<table class="moduleNav">
<?php
//check each module to find out the status of each module
//if unit is completed, displayed hyperlinks for all modules
//if unit is uncompleted, displayed hyperlinks for modules upto the latest one
//if unit is new, displayed the hyperlink for the first one
foreach ($modules as $cdeModule) {
?>
	<tr>
	<?php
	foreach ($cdeModule as $moduleValue) {
		if ($status == "Completed") {
	?>
		<?php
		if ($currentModule['CdeModule']['id'] == $moduleValue['CdeModule']['id']) {
		?>
			<td><a class="active" href="<?php echo $moduleValue['CdeModule']['id']; ?>"><?php echo $moduleValue['CdeModule']['title']; ?></a></td>
		<?php
		}else {
		?>
			<td><a href="<?php echo $moduleValue['CdeModule']['id']; ?>"><?php echo $moduleValue['CdeModule']['title']; ?></a></td>
		<?php
		}
		?>
	<?php
	}elseif ($status=="Uncompleted") {
		if ($moduleValue['CdeModule']['id'] > $continuemodule) {
	?>
		<td><?php echo $moduleValue['CdeModule']['title']; ?></td>
		<?php
		}else {
			if ($currentModule['CdeModule']['id'] == $moduleValue['CdeModule']['id']) {
		?>
			<td><a class="active" href="<?php echo $moduleValue['CdeModule']['id']; ?>"><?php echo $moduleValue['CdeModule']['title']; ?></a></td>
		<?php
			}else{
		?>
			<td><a href="<?php echo $moduleValue['CdeModule']['id']; ?>"><?php echo $moduleValue['CdeModule']['title']; ?></a></td>
		<?php
			}
		}
		?>
		<?php
		}else {
			if ($moduleValue['CdeModule']['id'] > $startmodule) {
		?>
			<td><?php echo $moduleValue['CdeModule']['title']; ?></td>
		<?php
			}else {
		?>
		<?php
			if ($currentModule['CdeModule']['id']==$moduleValue['CdeModule']['id']) {
		?>
			<td><a class="active" href="<?php echo $moduleValue['CdeModule']['id']; ?>"><?php echo $moduleValue['CdeModule']['title']; ?></a></td>
		<?php
			}else{
		?>
			<td><a href="<?php echo $moduleValue['CdeModule']['id']; ?>"><?php echo $moduleValue['CdeModule']['title']; ?></a></td>
		<?php
			}
		?>
		<?php
			}
		}
	}
	?>
		</tr>
	<?php
	}
	?>
</table>

<?php
/**
 * Display module detail
 */
$_SESSION['unitid']=$currentModule['CdeModule']['cde_unit_id'];
$_SESSION['currentmoduleid']=$currentModule['CdeModule']['id'];
$_SESSION['questiontype']=$currentModule['CdeModule']['questiontype'];
?>
<h2>Module instroduction</h2>
<p><?php echo $currentModule['CdeModule']['introduction']; ?></p>
<br />
<table class="module">
	<tr>
		<?php
		// Video panel
		if ($currentModule['CdeModule']['multimediaurl1']!='' || $currentModule['CdeModule']['multimediaurl1']!='') {
		?>
		<td>
			<table>
			<?php
			if ($currentModule['CdeModule']['multimediaurl1']!='') {
			?>
				<tr>
					<td>
						<h2>Video 1</h2>
						<br />
						<?php echo $element->youTubeVideo($currentModule['CdeModule']['multimediaurl1']); ?>
					</td>
				</tr>
			<?php
			}
			if ($currentModule['CdeModule']['multimediaurl2']!='') {
			?>
				<tr>
					<td>
						<h2>Video 2</h2>
						<br />
						<?php echo $element->youTubeVideo($currentModule['CdeModule']['multimediaurl2']); ?>
					</td>
				</tr>
			<?php
			}
			?>
			</table>
		</td>
		<?php
		}
		?>
		<td>&nbsp;</td>
		<td>
			<table>
				<tr>
					<td>
						<h2>Modules Information and Links</h2>
						<p><?php echo ($currentModule['CdeModule']['introduction']); ?></p>
						<br />
						<?php
						foreach($currentModule['CdeReferencelink'] as $link) {
							if (substr($link['url'],0,7)=="http://") {
						?>
							<a href="#" onclick="window.open('<?php echo $link['url']; ?>','windname','width=500,height=300,toolbar,scrollbars,resizable,menubar,status,location,top=0,left=0'); return false;"><?php echo $link['display']; ?></a><br />
						<?php
							}else {
						?>
							<a href="#"  onclick="window.open('http://<?php echo $link['url']; ?>','windname','width=500,height=300,toolbar,scrollbars,resizable,menubar,status,location,top=0,left=0'); return false;"><?php echo $link['display']; ?></a><br />
						<?php
								}
							}
						?>
					</td>
				</tr>
				<tr>
					<td>
						<h2>Question</h2>
						<p><?php echo $currentModule['CdeModule']['question']; ?></p>
					</td>
				</tr>
				<tr>
					<td>
						<h2>Answer</h2>
						<form name="theForm" method="post" action="../../checkanswer">
						<input name="submit" type="hidden" value="submitdata" />
						<?php
						if ($currentModule['CdeModule']['questiontype'] == 'SINGLE_ANSWER') {
							// Generate a list of radio button
							foreach($currentModule['CdeAnswer'] as $answer){
								// Check if answer is enable, admin check
								if ($userloggedinrole == 'admin') {
									if (count($useranswer)>0) {
										// Module is completed
										if ($answer['id'] == $useranswer[0]['cde_users_answers']['cde_answer_id']) {
										?>
											<label><input type="radio" name="answer" checked value="<?php echo $answer['id']; ?>" />&nbsp;&nbsp;&nbsp;&nbsp;<?php echo $answer['answercontent']; ?></label><br>
										<?php
										}else {
										?>
											<label><input type="radio" name="answer" value="<?php echo $answer['id']; ?>" />&nbsp;&nbsp;&nbsp;&nbsp;<?php echo $answer['answercontent']; ?></label><br>
										<?php
										}
									}else {
									?>
										<label><input type="radio" name="answer" value="<?php echo $answer['id']; ?>" />&nbsp;&nbsp;&nbsp;&nbsp;<?php echo $answer['answercontent']; ?></label><br>
									<?php
									}
								} else {
									if($answer['enabled'] == 1) {
										if (count($useranswer) > 0) {
											// Module is completed
											if ($answer['id'] == $useranswer[0]['cde_users_answers']['cde_answer_id']) {
											?>
												<label><input type="radio" name="answer" checked value="<?php echo $answer['id']; ?>" />&nbsp;&nbsp;&nbsp;&nbsp;<?php echo $answer['answercontent']; ?></label><br>
											<?php
											}else {
											?>
												<label><input type="radio" name="answer" value="<?php echo $answer['id']; ?>" />&nbsp;&nbsp;&nbsp;&nbsp;<?php echo $answer['answercontent']; ?></label><br>
											<?php
											}
										}else {
										?>
											<label><input type="radio" name="answer" value="<?php echo $answer['id']; ?>" />&nbsp;&nbsp;&nbsp;&nbsp;<?php echo $answer['answercontent']; ?></label><br>
										<?php
										}
									}
								}
							}
						}
						if ($currentModule['CdeModule']['questiontype'] == 'MULTIPLE_ANSWER'){
							// Generate a list of check box
							$arrayofanswer = array();
							if (count($useranswer) > 0) {
								//module is completed
								foreach ($useranswer as $value) {
									$arrayofanswer[] = $value['cde_users_answers']['cde_answer_id'];
								}
							}
							foreach($currentModule['CdeAnswer'] as $answer){
								if ($userloggedinrole == 'admin') {
									if (in_array($answer['id'],$arrayofanswer)) {
									?>
										<label><input type="checkbox" name="answer[]" checked value="<?php echo $answer['id']; ?>" />&nbsp;&nbsp;&nbsp;&nbsp;<?php echo $answer['answercontent']; ?></label><br>
									<?php
									}else {
									?>
										<label><input type="checkbox" name="answer[]" value="<?php echo $answer['id']; ?>" />&nbsp;&nbsp;&nbsp;&nbsp;<?php echo $answer['answercontent']; ?></label><br>
									<?php
									}
								} else {
									if($answer['enabled'] == 1) {
										if (in_array($answer['id'],$arrayofanswer)) {
										?>
											<label><input type="checkbox" name="answer[]" checked value="<?php echo $answer['id']; ?>" />&nbsp;&nbsp;&nbsp;&nbsp;<?php echo $answer['answercontent']; ?></label><br>
										<?php
										}else {
										?>
											<label><input type="checkbox" name="answer[]" value="<?php echo $answer['id']; ?>" />&nbsp;&nbsp;&nbsp;&nbsp;<?php echo $answer['answercontent']; ?></label><br>
										<?php
										}
									}
								}
							}
						}
						?>
						<br />
						<input type="submit" name="submitBtn" value="Submit answer" onclick="document.theForm.submitBtn.disabled=true;" />
						</form>
					</td>
				</tr>
			</table>
		</td>
	</tr>
</table>
<?php			
if (isset($_SESSION['submit'])) {
	unset($_SESSION['submit']);
	echo "<script type='text/javascript'>alert('".$_SESSION['messagesubmit']."'); </script>";
}
?>